package com.itheima.reggie.service.iml;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:test
 * @Create by: CAt
 * @Date;2021/9/2718:17
 */
@Service
public class SetmealServiceIml extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
@Autowired
private SetmealDishService setmealDishService;


    @Override
    public void tianjia(SetmealDto setmealDto) {
       this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    //修改回显
    @Override
    public SetmealDto xiugaihuixian(Long id) {
        Setmeal byId = this.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(byId,setmealDto);

        LambdaQueryWrapper<SetmealDish> wqewqs=new LambdaQueryWrapper<>();
        wqewqs.eq(SetmealDish::getDishId,setmealDto.getId());
        List<SetmealDish> list = setmealDishService.list(wqewqs);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    public void huixianbaocun(SetmealDto setmealDto) {
        this.updateById(setmealDto);


        LambdaQueryWrapper<SetmealDish> cccc=new LambdaQueryWrapper<>();
        cccc.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(cccc);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((mmm)->{
            mmm.setSetmealId(setmealDto.getId());

            return mmm;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
